package com.ruoyi.common.validator;

import com.ruoyi.common.annotation.CustomLength;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class CustomLengthValidator implements ConstraintValidator<CustomLength, String> {

    private int min;
    private int max;

    @Override
    public void initialize(CustomLength constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true; // null values are considered valid by default
        }
        int length = value.length();
        return length >= min && length <= max;
    }
}
